using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SheLife.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class HabitRecordController : ControllerBase
    {
        private readonly IHabitRecordService _habitRecordService;

        public HabitRecordController(IHabitRecordService habitRecordService)
        {
            _habitRecordService = habitRecordService;
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<HabitRecord>> GetHabitRecord(int id)
        {
            var record = await _habitRecordService.GetHabitRecordByIdAsync(id);
            if (record == null)
            {
                return NotFound();
            }
            return record;
        }

        [HttpGet("habit/{habitId}")]
        public async Task<ActionResult<IEnumerable<HabitRecord>>> GetHabitRecordsByHabitId(int habitId)
        {
            var records = await _habitRecordService.GetHabitRecordsByHabitIdAsync(habitId);
            return Ok(records);
        }

        [HttpGet("user/{userId}")]
        public async Task<ActionResult<IEnumerable<HabitRecord>>> GetHabitRecordsByUserId(int userId)
        {
            var records = await _habitRecordService.GetHabitRecordsByUserIdAsync(userId);
            return Ok(records);
        }

        [HttpGet("habit/{habitId}/daterange")]
        public async Task<ActionResult<IEnumerable<HabitRecord>>> GetHabitRecordsByDateRange(
            int habitId, 
            [FromQuery] DateTime startDate, 
            [FromQuery] DateTime endDate)
        {
            var records = await _habitRecordService.GetHabitRecordsByDateRangeAsync(habitId, startDate, endDate);
            return Ok(records);
        }

        [HttpGet("habit/{habitId}/monthly/{year}/{month}")]
        public async Task<ActionResult<object>> GetMonthlyCompletionStats(int habitId, int year, int month)
        {
            var stats = await _habitRecordService.GetMonthlyCompletionStatsAsync(habitId, year, month);
            return Ok(stats);
        }

        [HttpPost]
        public async Task<ActionResult<HabitRecord>> CreateHabitRecord(HabitRecord record)
        {
            var createdRecord = await _habitRecordService.CreateHabitRecordAsync(record);
            return CreatedAtAction(nameof(GetHabitRecord), new { id = createdRecord.Id }, createdRecord);
        }

        [HttpPost("batch")]
        public async Task<ActionResult<IEnumerable<HabitRecord>>> CreateHabitRecordsBatch(List<HabitRecord> records)
        {
            var createdRecords = await _habitRecordService.CreateHabitRecordsBatchAsync(records);
            return CreatedAtAction(nameof(GetHabitRecord), new { id = createdRecords[0].Id }, createdRecords);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateHabitRecord(int id, HabitRecord record)
        {
            if (id != record.Id)
            {
                return BadRequest();
            }

            await _habitRecordService.UpdateHabitRecordAsync(record);
            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteHabitRecord(int id)
        {
            await _habitRecordService.DeleteHabitRecordAsync(id);
            return NoContent();
        }
    }
}